package com.blkj.iam.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blkj.iam.common.annotation.Log;
import com.blkj.iam.common.annotation.RepeatSubmit;
import com.blkj.iam.common.enums.LogModuleEnum;
import com.blkj.iam.common.result.PageResult;
import com.blkj.iam.common.result.Result;
import com.blkj.iam.system.model.form.ClientForm;
import com.blkj.iam.system.model.query.ClientPageQuery;
import com.blkj.iam.system.model.vo.ClientPageVO;
import com.blkj.iam.system.service.ClientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 应用控制层
 * @Author: jeecg-boot
 * @Date: 2025-04-09
 * @Version: V1.0
 */
@Tag(name = "15.应用接口")
@RestController
@RequestMapping("/api/v1/client")
@Slf4j
public class ClientController {

    @Resource
    private ClientService clientService;


    @Operation(summary = "应用分页列表")
    @GetMapping("/page")
    @Log(value = "应用分页列表", module = LogModuleEnum.CLIENT)
    public PageResult<ClientPageVO> getClientPage(@Valid ClientPageQuery queryParams) {
        IPage<ClientPageVO> result = clientService.getClientPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "应用表单数据")
    @GetMapping("/{clientId}/form")
    @Log(value = "应用表单数据", module = LogModuleEnum.CLIENT)
    public Result<ClientForm> getClientForm(@Parameter(description = "应用ID") @PathVariable Long clientId) {
        ClientForm formData = clientService.getClientFormData(clientId);
        return Result.success(formData);
    }

    @Operation(summary = "新增应用")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:client:add')")
    @RepeatSubmit
    @Log(value = "新增应用", module = LogModuleEnum.CLIENT)
    public Result<?> saveClient(@RequestBody @Valid ClientForm clientForm) {
        boolean result = clientService.saveClient(clientForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改应用")
    @PutMapping(value = "/{clientId}")
    @PreAuthorize("@ss.hasPerm('sys:client:edit')")
    @Log(value = "修改应用", module = LogModuleEnum.CLIENT)
    public Result<Void> updateClient(@Parameter(description = "应用ID") @PathVariable Long clientId, @RequestBody @Valid ClientForm clientForm) {
        boolean result = clientService.updateClient(clientId, clientForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除应用")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:client:delete')")
    @Log(value = "删除应用", module = LogModuleEnum.CLIENT)
    public Result<Void> deleteClients(@Parameter(description = "应用ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = clientService.deleteClients(ids);
        return Result.judge(result);
    }
}
